package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class SignoffServlet extends HttpServlet {

    private static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";

    private Account account;
    private List<Product> myList;
    private boolean authenticated;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        //持久化购物车
        session.setAttribute("username", null);
        session.setAttribute("cart", null);

        //清空session
//        session.invalidate();
        //重新赋值
        account = new Account();
        myList = null;
        authenticated = false;
        //放入session
        session.setAttribute("account", account);
        session.setAttribute("myList", myList);
        session.setAttribute("authenticated", authenticated);

        Account account = (Account) session.getAttribute("account");
        if(account != null){
            HttpServletRequest httpRequest= request;
            String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + strBackUrl + MAIN;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        request.getRequestDispatcher(MAIN).forward(request, response);
    }
}
